package com.liangtian.mdm.material.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liangtian.mdm.material.dao.WlwlFieldSelectValueDao;
import com.liangtian.mdm.material.model.WlwlFieldSelectValue;
import com.liangtian.mdm.material.service.WlwlFieldSelectValueService;

@Service
public class WlwlFieldSelectValueServiceImpl implements WlwlFieldSelectValueService {

    @Autowired
    private WlwlFieldSelectValueDao selectValDao;

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public PageInfo getListByCond(Map<String, Object> map) {
        PageInfo<List<WlwlFieldSelectValue>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）

            List<WlwlFieldSelectValue> list = selectValDao.getListByCond(map);
            pageInfo = new PageInfo(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public void insertWlwlFieldSelectvalue(WlwlFieldSelectValue value) {
        selectValDao.insertWlwlFieldSelectvalue(value);
    }

    @Override
    public int getCountByMap(WlwlFieldSelectValue value) {
        Map<String, Object> map = new HashMap<>();
        map.put("field", value.getField());
        map.put("fieldValue", value.getFieldValue());
        map.put("fieldText", value.getFieldText());
        return selectValDao.getCountByMap(map);
    }

    @Override
    public WlwlFieldSelectValue getValueById(WlwlFieldSelectValue value) {
        return selectValDao.getValueById(value);
    }

    @Override
    public int getCountByMapToEdit(WlwlFieldSelectValue value) {
        Map<String, Object> map = new HashMap<>();
        map.put("field", value.getField());
        map.put("fieldValue", value.getFieldValue());
        map.put("fieldText", value.getFieldText());
        map.put("id", value.getId());
        return selectValDao.getCountByMapToEdit(map);
    }

    @Override
    public void updateWlFieldSelVal(WlwlFieldSelectValue value) {
        selectValDao.updateWlFieldSelVal(value);
    }

}
